package cn.com.gridinfo.sooc.core.expand.callname.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.YingDaoAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 签到详情——应到 fragment
 * Created by yingchunli on 2015/12/8.
 */
public class YingDaoFragment extends BaseSoocFragment {

    @InjectView(R.id.signinfo_list)
    UltimateRecyclerView signinfoList;//列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    YingDaoAdapter adapter;//适配器
    String ktid;//从activity传递过来的参数
    View view;//当前界面
    CallNameDao dao;
    @InjectView(R.id.text_siginfo)
    TextView textSiginfo;//应到人数text
    @InjectView(R.id.count_ren_layout)
    LinearLayout countRenLayout;//应到人数的布局
    @InjectView(R.id.count_ren_signinfo)
    TextView countRen;//人数
    int maxLastPositon;//数据最后位置
    @InjectView(R.id.blank_view)
    RelativeLayout blankLayout;//空白页布局
    @InjectView(R.id.url_load)
    TextView url_blank;//

    public static YingDaoFragment newInstance(String info) {
        Bundle args = new Bundle();
        YingDaoFragment fragment = new YingDaoFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断Fragment中的ListView时候存在，判断该Fragment时候已经正在前台显示  通过这两个判断，就可以知道什么时候去加载数据了
        if (isVisibleToUser && isVisible()) {
            Log.d("yindao adapter=" + adapter);
            initData(); //加载数据的方法
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    void initData() {
        //获取应到列表
        PageConst.pageNo = 1;
        PageConst.pageSize = 20;
        dao.getSignStudentList(ktid, PageConst.pageNo, PageConst.pageSize, null, CodeConst.EXPAND_GET_STUDENT_LIST_PUSHDOWN);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_signinfo_list, null);
        ButterKnife.inject(this, view);
        ktid = getArguments().getString("info");
        url_blank.setText(Arad.preferences.getString(LocalMark.SERVER_URL.getName())+"上传名单。");
        linearLayoutManager = new LinearLayoutManager(getActivity());
        signinfoList.setLayoutManager(linearLayoutManager);
        dao = new CallNameDao(this);
        countRen.setTextColor(getResources().getColor(R.color.orange));//设置人数的颜色
        countRenLayout.setVisibility(View.GONE);
        initView();
        blankLayout.setVisibility(View.GONE);
        return view;
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_GET_STUDENT_LIST_PUSHDOWN://获取列表  下拉请求
                        signinfoList.setRefreshing(false);
                        if (dao.getStudentList() != null && !dao.getStudentList().isEmpty()) {
                            blankLayout.setVisibility(View.GONE);
                            signinfoList.hideEmptyView();
                            countRenLayout.setVisibility(View.VISIBLE);
                            countRen.setText(dao.getMdrs() + "");
                            textSiginfo.setText("应到人数: ");
                            adapter.refresh(dao.getStudentList());
                            adapter.loadNoDataView();
                        } else {
                            //无数据处理
                            blankLayout.setVisibility(View.VISIBLE);
                            countRenLayout.setVisibility(View.GONE);
                            signinfoList.showEmptyView();
                        }

//                        if (dao.getStudentList().size() > (PageConst.pageSize - 1)) {
//                            adapter.loadMoreDataView();
//                        } else {
                            //数据小于当前页的话则不显示加载更多

//                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
//                            linearLayoutManager.scrollToPosition(maxLastPositon);
//                        }
                        break;
                    case CodeConst.EXPAND_GET_STUDENT_LIST_PULLUP://获取列表  上拉请求
                        if (dao.getStudentList() != null && !dao.getStudentList().isEmpty()) {
                            adapter.loadmore(dao.getStudentList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "请求数据异常");
            Log.e("数据异常原因：" + e);
        }
    }

    /**
     * 初始化界面数据
     */
    void initView() {
        List<Student> studentList = new ArrayList<>();
        adapter = new YingDaoAdapter(studentList, getActivity());
        signinfoList.setAdapter(adapter);
        signinfoList.enableLoadmore();
        signinfoList.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PageConst.pageNo = 1;//首页
                dao.getSignStudentList(ktid, PageConst.pageNo, PageConst.pageSize, null, CodeConst.EXPAND_GET_STUDENT_LIST_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        signinfoList.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {

//                if (itemsCount > (PageConst.pageSize - 1)) {
//                    maxLastPositon = maxLastVisiblePosition;
//                    dao.getSignStudentList(ktid, ++PageConst.pageNo, PageConst.pageSize, null, CodeConst.EXPAND_GET_STUDENT_LIST_PULLUP);
//                } else {
//                    if (signinfoList != null)
//                        adapter.loadNoDataView();
//                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
//                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
